<?php
/**
 * 登录日志模型.
 * User: Administrator
 * Date: 2017/4/28
 * Time: 10:57
 */
namespace app\backend\model;
use think\Model;

class LoginLog extends Model
{
    private $_loginLog = 'z_tp_login_log';

    public function __construct() 
    {
        parent::__construct();
    }

    public function createLoginLog($memberId, $loginIp, $loginLocation, $loginBrowser, $loginOS)
    {
        if (!is_numeric($memberId) || !is_numeric($loginIp)) {
            return false;
        }

        $data = [
            'mid'            => $memberId,
            'login_ip'       => $loginIp,
            'login_location' => $loginLocation,
            'login_browser'  => $loginBrowser,
            'login_os'       => $loginOS,
            'login_time'     => time(),
        ];

        $res = $this->table($this->_loginLog)->insertGetId($data);
        return $res;
    }

    /**
     * 获取登录日志
     * @date   2017-04-28
     * @author wuhao
     *
     * @param  int   $mid   登录人id
     *
     * @return array
     */
    public function getLoginLog($mid)
    {
        if (!$mid || !is_numeric($mid)) {
            return false;
        }
        $res = $this->setTable($this->_loginLog)->setTable($this->_loginLog)->where(['mid' => $mid])->select();
        return $res ?: [];
    }

    /**
     * 获取最近一条登录日志
     * @date   2017-04-28
     * @author wuhao
     *
     * @param  int   $mid   登录人id
     *
     * @return array
     */
    public function getLastLoginLog($mid)
    {
        if (!$mid || !is_numeric($mid)) {
            return false;
        }
        $res = $this->setTable($this->_loginLog)->setTable($this->_loginLog)->where(['mid' => $mid])->order('id desc')->limit(1)->find();
        return $res ?: [];
    }
}